<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['basic']=$this->plugin_setting('basic');
$return['banner']=$return['basic']['hdps'];
if($_RQ['op']=='main'){
	/*******main*******/
	//路线
	$id=$_RQ['id'];
	if(!$id){
		exi('该路线可能已下架或已删除','error');
	}
	$way=pdo_get('jy_tour_way',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($way){
		$images=json_decode($way['images'],true);
		$merch=pdo_get('jy_tour_merch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$way['merchid']));
		$way['merch']=$merch;
		$way['images']=$images;
	}
	$return['way']=$way;
	$return['info']['way']=$way;
	exi($return);
}
//提交
if($_RQ['op']=='submit'){
	$post=get_postdata();
	$way=$post['way'];
	//print_r($post);exit;
	if(!$post){
		exi('请完善相关信息！','error');
	}
	if(!$post['time']){
		exi('请选择出发时间！','error');
	}
	if(!$post['name']){
		exi('请选择联系人姓名！','error');
	}
	if(!$post['mobile']){
		exi('请填写联系电话！','error');
	}
	//生成订单
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'merchid'=>$way['merchid'],
		'way_id'=>$way['id'],
		'go_time'=>$post['time'],
		'buynum'=>$post['buynum']?$post['buynum']:1,
		'name'=>$post['name'],
		'mobile'=>$post['mobile'],
		'price'=>($post['buynum']?$post['buynum']:1)*$way['price'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_tour_order',$insert);
	$id=pdo_insertid();
	$params['fee']=$insert['price'];
	$params['title']='旅游订单';
	$params['id']=$id;
	$params['plugin']='jy_tour';
	$params['back_url']='jy_tour/way.book/payresult';
	$return['payid']=cfc('pay')->create_pay_order($params);
	exi($return);
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_tour_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_tour_order',array('status'=>'1','fee'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_tour/order.list//status=1&id='.$order['id']);
}